<?php

namespace App\Models\CacheModel\privatearea;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class PrivateAreaMallGoodsAttr extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.private_area_mall_goods_attr';

    protected $cacheModel = 'privatearea.mall_good_attr';

    /**
     * 获取商品规格列表
     * @param int $gid
     * @param int $page
     * @param int $pageSize
     * @param array $fields
     * @return array
     * @throws \Exception
     */
    public function getGoodAttrList(int $gid, int $page, int $pageSize, array $fields = [])
    {
        $data = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $gid)
            ->setPage($page)
            ->setPageSize($pageSize)
            ->setSort('desc')
            ->setListKeyType(0)
            ->getListPageDataIds(function ($ids = [], $isCache = true, $sortType = 'desc') use ($gid)
            {
                try
                {
                    $data = $this->where(['gid' => $gid, 'status' => 1])->order('sort desc,id desc')->column('sort', 'id');
                    $resData = [];
                    foreach ($data as $id => $sort)
                    {
                        $resData[$id] = ['id' => $id, 'sort' => $sort,];
                    }
                    return $resData;
                }
                catch (\Exception $e)
                {
                    $data = [];
                }
                return $data;
            }, 'sort');

        $dataLiist = $this->batchGetInfoByIds(array_keys($data['data']), $fields);
        $resDataList = [];
        foreach ($data['data'] as $id => $sort)
        {
            if (isset($dataLiist[$id]))
            {
                $resDataList[] = $dataLiist[$id];
            }
        }
        $data['data'] = $resDataList;
        return $data;
    }

    /**
     * 更新数据和缓存
     * @param int $id
     * @param array $data
     * @return void
     */
    public function UpdateData(int $id, array $data)
    {
        try
        {
            $data['update_time'] = time();
            $this->where(['id' => $id])->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->setCacheTime(30)->setCache($data);
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
        }
    }

    /**
     * 更新数据库
     * @param int $id
     * @param array $data
     * @return void
     */
    public function UpdateDataByDb(int $id, array $data)
    {
        try
        {
            $this->where(['id' => $id])->update($data);
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
        }
    }

    /**
     * 更新 商品库存 缓存
     * @param int $id
     * @return false|int
     * @throws \Exception
     */
    public function goodInventoryChangeCache(int $id, int $inc = -1)
    {
        $resInventory = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->setCacheTime(100)->setCacheInc('inventory', 'details', $inc);

        return $resInventory;
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {   // 获取未删除 的数据
            $data = $this
                ->where(['id' => ['in', $ids]])
                ->column('id,status,sort,gid,name,img,or_price,now_price,inventory,num,create_time,update_time,shipping_type', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'gid', 'status', 'sort', 'inventory', 'num', 'create_time', 'update_time', 'shipping_type']);
        }
        return $data;
    }
}
